package com.hcbd.tdwt.ui.fuda.common.fragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.ui.fuda.common.activity.AddContactActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.SendMsgActivity_;
import com.hcbd.tdwt.ui.fuda.common.activity.SosActivity_;

import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.GlobalParams;
import com.raizlabs.android.dbflow.sql.language.SQLite;



import com.hcbd.tdwt.ui.xingyu.adapter.ContactMsgAdapter;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact_Table;
import com.hcbd.tdwt.ui.xingyu.vo.UserMessageVO;
import com.hcbd.tdwt.ui.xingyu.vo.UserMessageVO_Table;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_communicaion_fuda)
public class CommunicationFragment extends BaseFragment {
    @ViewById
    TextView title;
    @ViewById
    ImageButton headLeft;
    @ViewById(R.id.contact_rv)
    RecyclerView contactRv;
    @ViewById
    ImageButton sosBtn,addContactImg,addMsgBtn;

    @ViewById
    RelativeLayout topRL;
    private ContactMsgAdapter contactMsgAdapter;
    private List<BoxContact> contactList;
    private final int refresh_code=5;

    @AfterViews
    protected void init() {
        if(BdToolFactory.app_type==BdToolFactory.HAI_GE_BG_TYPE){
            title.setText("报文通信");
            sosBtn.setVisibility(View.GONE);
            addMsgBtn.setVisibility(View.GONE);
            headLeft.setVisibility(View.GONE);
            contactRv.setBackground(null);
            topRL.setBackground(ContextCompat.getDrawable(Objects.requireNonNull(getContext()),R.drawable.chat_top_bg_flag_haige));
            addContactImg.setBackground(ContextCompat.getDrawable(this.getContext(),R.drawable.add_contact_haige));
        }else{
            title.setText("消息列表");
        }
        headLeft.setVisibility(View.GONE);
        contactList= SQLite.select().from(BoxContact.class).orderBy(BoxContact_Table.recentCommunicationTime, false).queryList();
        initContactData();
        initMsgRegister();

    }

    public void initContactData(){
        if(contactMsgAdapter==null){
            contactMsgAdapter=new ContactMsgAdapter(this.getContext(),contactList);
            contactMsgAdapter.setHasStableIds(true);
        }else{
            contactMsgAdapter.notifyDataSetChanged(contactList);
            return;
        }
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 1);
        contactRv.setLayoutManager(gridLayoutManager);
        contactRv.setAdapter(contactMsgAdapter);
        contactMsgAdapter.setOnItemClickListener(new ContactMsgAdapter.OnItemClickListener() {
            @Override
            public void onClick(BoxContact boxContact,View view) {
                Intent intent=new Intent();
                intent.setClass(getContext(), SendMsgActivity_.class);
                if(GlobalParams.rdCentreNumber.equals(boxContact.getBoxId())){
                    intent.putExtra("receiverNumber",GlobalParams.rdCentreNumber);
                    intent.putExtra("entryMode", Constant.ENTRY_SEND_CENTER_NUMBER);
                }else{
                    intent.putExtra("receiverNumber",boxContact.getBoxId());
                    intent.putExtra("entryMode",Constant.ENTRY_SEND_USER_MSG);
                }
                intent.putExtra("receiverName",boxContact.getName());
                startActivityForResult(intent,refresh_code);
            }
            @Override
            public void onEdit(BoxContact boxContact) {
                Intent intent=new Intent();
                intent.setClass(getContext(), AddContactActivity_.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("boxContact",boxContact);
                intent.putExtras(bundle);
                intent.putExtra("entryMode",1);
                startActivityForResult(intent,refresh_code);
            }
            @Override
            public void onDel(BoxContact boxContact) {
                try {
                    boxContact.delete();
                    SQLite.delete(UserMessageVO.class)
                            .where(UserMessageVO_Table.receiverNumber.eq(boxContact.getBoxId()))
                            .execute();
                    Constant.homePageIsRefresh=true;
                }catch (Exception e){
                    e.printStackTrace();
                }
                selectAll();
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(UserMessageVO userMessage){
        selectAll();
    }
    @Override
    public void onSwitch() {
        super.onSwitch();
        if(Constant.msgPageIsRefresh&&contactMsgAdapter!=null){
            selectAll();
        }
    }
    public void selectAll(){
        contactList= SQLite.select().from(BoxContact.class).orderBy(BoxContact_Table.recentCommunicationTime, false).queryList();
        initContactData();
    }
    @Override
    public void  onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            //不可见
            hideInputKeyBord();
        }
    }
    @Click
    void addMsgBtn(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), SendMsgActivity_.class);
        startActivityForResult(intent,refresh_code);
    }
    @Click
    void sosBtn(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), SosActivity_.class);
        startActivity(intent);
    }
    @Click
    void addContactImg(){
        Intent intent=new Intent();
        intent.setClass(Objects.requireNonNull(getContext()), AddContactActivity_.class);
        startActivityForResult(intent,refresh_code);
    }
    @OnActivityResult(refresh_code)
    public void resultMsgList(int resultCode){
        if(resultCode== Activity.RESULT_OK){
            selectAll();
        }
    }
}
